#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>

using namespace std;

char dna[59][1001];
char vis[1001][32];

int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
	{
		int m, n;
		scanf("%d%d", &m, &n);
		memset(vis, 0, sizeof(vis));
		for (int i = 0; i < m; ++i)
		{
			scanf("%s", dna[i]);
			for (int j = 0; j < n; ++j)
			{
				vis[j][dna[i][j] - 'A']++;
			}
		}

		int cnt = 0;
		for (int i = 0; i < n; ++i)
		{
			int max = -1;
			int maxj = -1;
			for (int j = 0; j < 26; ++j)
			{
				if(max < vis[i][j])
				{
					max = vis[i][j];
					maxj = j;
				}
			}
			cnt += m - max;
			printf("%c", maxj + 'A');
		}
		printf("\n");
		printf("%d\n", cnt);
	}
	return 0;
}
